//
// Created by Incredible on 17/3/23.
//

#ifndef GRAPHTHEORY_READGRAPH_H
#define GRAPHTHEORY_READGRAPH_H

#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
#include <sstream>

using namespace std;

//读取文件的模板类
template<typename Graph>
class ReadGraph {

public:

    //        需要被加边的图的引用   ，    文件名
    ReadGraph(Graph &graph, const string &fileName) {

        //读取文件数据内容
        //第一步:打开文件
        ifstream file(fileName);
        string line;
        int V, E;

        //第二步:判断问价打开是否正确
        assert(file.is_open());

        //第三步:读取文件内容
        assert(getline(file, line));

        //流转换
        stringstream ss(line);
        ss >> V >> E;

        //判断读取的顶点数是否相等
        assert(V == graph.V());

        for (int i = 0; i < E; ++i) {
            assert(getline(file, line));
            stringstream ss(line);
            int a, b;
            ss >> a >> b;

            assert(a >= 0 && a < V);
            assert(b >= 0 && b < V);
            graph.addEdge(a, b);

        }
    }
};


#endif //GRAPHTHEORY_READGRAPH_H
